import { TencentService } from '@/core/tencent.service';
import { VerifyService } from '@/core/verify.service';
import { Public, RealIp } from '@/decorators';
import { VerifyCheckDto } from '@/dtos';
import { halt, ok } from '@/utils';
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { BaseController } from './base.controller';

@Controller('verify')
export class VerifyController extends BaseController {
  constructor(
    private readonly verifyService: VerifyService,
    private readonly tencentService: TencentService,
  ) {
    super();
  }

  @Public()
  @Get(':mobile')
  async getImage(@Param('mobile') mobile: string) {
    return ok(await this.verifyService.getImage(mobile));
  }

  @Public()
  @Post('check')
  async check(@Body() dto: VerifyCheckDto, @RealIp() ip: string) {
    const result = await this.tencentService.describeCaptchaResult({
      UserIp: ip,
      Randstr: dto.randstr,
      Ticket: dto.ticket,
    });
    if (result?.CaptchaCode !== 1) {
      return halt(result?.CaptchaMsg ?? '验证失败');
    }
    // if (!(await this.verifyService.verify(dto.mobile, dto.x))) {
    //   return halt('验证失败');
    // }
    return ok(await this.verifyService.sendSms(dto.mobile));
  }
}
